package ntut.andengine.game;

import javax.microedition.khronos.opengles.GL10;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.DelayModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel2 extends GameLevel{
	
	TextureRegion blueTextureRegion;
	TextureRegion redTextureRegion;
	GameLevel mLevel;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		
		BitmapTexture bitmapTexture = new BitmapTexture(128, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		blueTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "blue_tap.png", 0, 0);
		redTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "red_tap.png", 57, 0);
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		
		mEngine.getTextureManager().loadTexture(bitmapTexture);	
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);
		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {
		
		PhysicalFactory.CreateWall(this, new Vector2(240, 280), 480, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(240, 480), 480, 20, ComponentColor.Red);

        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(240, 640));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					
					startActivity(GameLevel2.this, GameLevel12.class);
				}
			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				// TODO Auto-generated method stub
				
			}
		
		});
        
        // hint
        Sprite blueTap = new Sprite(300, 150, blueTextureRegion);
        blueTap.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        Sprite redTap = new Sprite(300, 150, redTextureRegion);
        redTap.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
        blueTap.registerEntityModifier(new LoopEntityModifier(new SequenceEntityModifier(
        		new AlphaModifier(1, 0, 1),
        		new DelayModifier(1),
        		new AlphaModifier(1, 1, 0)
        )));
        redTap.registerEntityModifier(new LoopEntityModifier(new SequenceEntityModifier(
        		new AlphaModifier(1, 1, 0),
        		new DelayModifier(1),
        		new AlphaModifier(1, 0, 1)
        )));
        // hint
        
        mScene.attachChild(blueTap);
        mScene.attachChild(redTap);
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(240, 120), ComponentColor.Blue);
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);	

		return mScene;
	}

}
